#include <iostream>
using namespace std;
#define M 100

//邻接矩阵存储结构
struct AMGraph {
    char vexs[M];
    int arcs[M][M];
    int vexnum, arcnum;
    mutable int visited[M];
};

//从顶点v出发广度优先遍历图G
void BFS(AMGraph &G, int v)
{
    //访问顶点v
    cout << G.vexs[v] << " ";
    G.visited[v] = 1;
    //利用队列进行广度优先搜索
    int q[M + 2];
    int front = 0, rear = 0;
    q[rear++] = v;
    while(front != rear) {
        v = q[front++];
        for(int w = 0; w < G.vexnum; w++) {
            if(G.arcs[v][w] && !G.visited[w]) {
                cout << G.vexs[w] << " ";
                G.visited[w] = 1;
                q[rear++] = w;
            }
        }
    }
}

//广度优先搜索遍历图G
void BFSTraverse(AMGraph &G)
{
    //初始化顶点访问标志未访问的
    for (int i = 0; i < G.vexnum; i++)
        G.visited[i] = 0;
    //选择一个未访问的顶点作为起点，开始广度优先搜索
    for(int v = 0; v < G.vexnum; v++)
        if(!G.visited[v])
            BFS(G, v);
}

int main()
{
    AMGraph G = {
        {'A','B','C','D','E'},
        {
            {0, 1, 1, 0, 0},
            {0, 0, 0, 0, 1},
            {0, 1, 0, 1, 0},
            {0, 1, 0, 0, 0},
            {1, 0, 0, 1, 0},
        },
        5,
    };
BFSTraverse(G);

    return 0;
}